/*
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.seo.serp.logic;

import java.sql.Timestamp;
import java.util.List;

import cc.aileron.commons.util.CastUtils;
import cc.aileron.commons.util.SkipList;
import cc.aileron.generic.Id;
import cc.aileron.seo.serp.model.Serp;
import cc.aileron.seo.serp.model.SerpContent;

import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * @author Aileron
 */
public class SearchEngineLogicGoogle implements SearchEngineLogic
{
    @Override
    public Serp get(final HtmlPage page)
    {
        final List<SerpContent> contents = new SkipList<SerpContent>();
        final Timestamp searchTime = new Timestamp(System.currentTimeMillis());
        final Serp serp = new Serp()
        {
            @Override
            public List<SerpContent> getContents()
            {
                return contents;
            }

            @Override
            public Timestamp getSearchTime()
            {
                return searchTime;
            }
        };
        final List<HtmlAnchor> list = CastUtils.cast(page.getByXPath("//*[@class='g']/*[@class='r']/a"));

        int i = 0;
        for (final HtmlAnchor anchor : list)
        {
            i += 1;

            final String url = anchor.getAttributeValue("href");
            final String label = anchor.getTextContent();
            final int rank = i;
            final SerpContent content = new SerpContent()
            {

                @Override
                public String href()
                {
                    return url;
                }

                @Override
                public Id id()
                {
                    return id;
                }

                @Override
                public void id(final Id id)
                {
                    this.id = id;
                }

                @Override
                public String label()
                {
                    return label;
                }

                @Override
                public int rank()
                {
                    return rank;
                }

                @Override
                public Timestamp searchTime()
                {
                    return searchTime;
                }

                private Id id;
            };
            contents.add(content);
        }
        return serp;
    }

    /**
     * logger
     */
    // private final Logger logger = LoggerFactory.getLogger(this.getClass());
}
